#!/usr/bin/env python
#coding:utf-8

import redis
import Global

class RedisHelper(object):
    
    __consistHash = Global.Global.REDISHASHRING
    
    @staticmethod
    def GetRandomConn(key):
        try:
            ip,port = RedisHelper.__consistHash.get_node(key)
            redis.ConnectionPool
            pool = redis.ConnectionPool(host=ip, port=6379, db=0)
            conn = redis.Redis(connection_pool=pool)
            return conn
        except Exception,e:
            raise Exception(u'在哈希环上获取redis节点失败')
    
    @staticmethod
    def Get(self,key):
        try:
            conn = RedisHelper.GetRandomConn(key)
            return conn.get(key)
        except Exception,e:
            raise Exception(u'在redis中获取数据失败')
        
    @staticmethod
    def Set(key,value):
        try:
            conn = RedisHelper.GetRandomConn(key)
            return conn.set(key, value)
        except Exception,e:
            raise Exception(u'在redis中设置数据失败')